﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Domain.Campaigns;
using iOak.Business.Campaigns;
using iOak.Common;
using iOak.Domain.Resumes;
using iOak.Business.Resumes;
using iOak.Business.Users;
using iOak.Common.Untity;

namespace iOak.Web.Administration
{
    public partial class ApplicationList : BasePage
    {
        private WorkAssistant _workassistant = new WorkAssistant();

        private int SearchMode
        {
            set { this.ViewState["SearchMode"] = value; }
            get { return int.Parse(this.ViewState["SearchMode"].ToString()); }
        }

        #region 页面事件
        /// <summary>
        /// 页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            pagerControl.PageIndexChange += new Modules.PagerControl.OnPageIndexChangeHandler(pagerControl_PageIndexChange);
            if (!Page.IsPostBack)
            {
                BindCampaignList();
                if (!String.IsNullOrWhiteSpace(this.Request.QueryString["CampaignId"]))
                {
                    Guid campaignId = Guid.Parse(this.Request.QueryString["CampaignId"]);
                    CampaignService campaignService = new CampaignService();
                    Campaign campaign = campaignService.Retrieve(campaignId);
                    if (campaign != null)
                    {
                        lblApplicationName.Text = campaign.Subject + "的" + lblApplicationName.Text;
                    }
                    ddlApplication.SelectedValue = campaignId.ToString();
                }
                SearchMode = 0;
                if (!String.IsNullOrWhiteSpace(this.Request.QueryString["PageMode"]))
                {
                    SearchMode = int.Parse(this.Request.QueryString["PageMode"]);
                }
                BindApplicationList();
            }
            if (SearchMode == 0)
            {
                hlAll.CssClass = "menu_select";
                hlSelf.CssClass = String.Empty;
            }
            else
            {
                hlSelf.CssClass = "menu_select";
                hlAll.CssClass = String.Empty;
            }
        }

        protected void pagerControl_PageIndexChange(object serder, EventArgs e)
        {
            BindApplicationList();
        }

        /// <summary>
        /// 行绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvApplication_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ApplicationBase applicationBase = e.Row.DataItem as ApplicationBase;
                Label lblCompanyName = e.Row.FindControl("lblCompanyName") as Label;
                CompanyService companyService = new CompanyService();
                Company company = companyService.Retrieve(applicationBase.CompanyId);
                if (company != null)
                {
                    lblCompanyName.Text = company.CompanyName;
                }
                ApplicationCandidateService applicationCandidateService = new ApplicationCandidateService();
                Label lblCandidateCount = e.Row.FindControl("lblCandidateCount") as Label;
                lblCandidateCount.Text = applicationCandidateService.GetCount(applicationId: applicationBase.ApplicationId).ToString();
                UserService userService = new UserService();
                var appliedBy = userService.GetUserById(applicationBase.AppliedBy);
                if (appliedBy != null)
                {
                    Label lblAppliedBy = e.Row.FindControl("lblAppliedBy") as Label;
                    lblAppliedBy.Text = appliedBy.FullName;
                }
                HyperLink hlPreview = e.Row.FindControl("hlPreview") as HyperLink;
                hlPreview.NavigateUrl = "ApplicationCandidateList.aspx?applicationId=" + applicationBase.ApplicationId.ToString();
                Label lblStatus = e.Row.FindControl("lblStatus") as Label;
                lblStatus.Text = ResourceHelper.GetApplicationStatusName((int)applicationBase.Status);
            }
        }

        protected void btnPostBack_Click(object sender, EventArgs e)
        {
            switch (hidEventName.Value)
            {
                case "changeListMode":
                    gvApplication.PageIndex = 0;
                    SearchMode = int.Parse(hidEventArguments.Value);
                    BindApplicationList();
                    break;
            }
        }

        protected void ddlApplication_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvApplication.PageIndex = 0;
            BindApplicationList();
        }
        #endregion

        #region 处理函数

        /// <summary>
        /// 绑定Campaign列表
        /// </summary>
        private void BindCampaignList()
        {
            CampaignService campaignService = new CampaignService();
            List<Campaign> campaignList = campaignService.LoadAll();
            ddlApplication.DataSource = campaignList;
            ddlApplication.DataTextField = "Subject";
            ddlApplication.DataValueField = "CampaignId";
            ddlApplication.DataBind();
            ddlApplication.Items.Insert(0, new ListItem("", ""));
        }

        /// <summary>
        /// 绑定申请书列表
        /// </summary>
        /// <param name="mode">0=全部;1=当前人员的</param>
        private void BindApplicationList()
        {
            List<ApplicationBase> applicationBaseList;
            ApplicationService applicationService = new ApplicationService();
            if (SearchMode == 0)
            {
                if (ddlApplication.SelectedIndex != 0)
                {
                    applicationBaseList = applicationService.GetListByCampaignId(Guid.Parse(ddlApplication.SelectedValue));
                }
                else
                {
                    applicationBaseList = applicationService.LoadAll();
                }
            }
            else
            {
                if (ddlApplication.SelectedIndex != 0)
                {
                    applicationBaseList = applicationService.GetListByCheckedBy(_workassistant.CurrentUser.UserId, Guid.Parse(ddlApplication.SelectedValue));
                }
                else
                {
                    applicationBaseList = applicationService.GetListByCheckedBy(_workassistant.CurrentUser.UserId);
                }
            }
            //去掉未申报的申请书，并排序
            var dataSource = applicationBaseList.Where(item => item.Status != (int)ApplicationStatus.Open).OrderByDescending(item => item.AppliedDate).ToList();
            pagerControl.BindData(gvApplication, dataSource);
        }

        #endregion
    }
}